package com.jc.oss.service;

import com.jc.oss.adapter.StorageAdapter;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

/**
 * @Description:
 * @Author: ASL_ly
 * @Package: com.jc.oss.service.impl
 * @Project: jc-club
 * @Date: 2024/10/26  14:45
 */
@Service
public class FileService {

    final private StorageAdapter storageAdapter;
    public FileService(StorageAdapter storageAdapter) {
        this.storageAdapter = storageAdapter;
    }


    /**
     * 列出所有的bucket
     * @return
     * @throws Exception
     */

    public List<String> getAllBucket(){
        return storageAdapter.getAllBucket();
    }

    /**
     * 获取文件路径
     * @param bucketName
     * @param objectName
     * @return
     */
    public String getUrl(String bucketName, String objectName){
        return storageAdapter.getUrl(bucketName,objectName);
    }


    /**
     * 上传文件
     * @param uploadFile
     * @param bucket
     * @param objectName
     * @return
     */
    public String uploadFile(MultipartFile uploadFile, String bucket, String objectName) {
        storageAdapter.uploadFile(uploadFile,bucket,objectName);
        objectName = objectName + "/" + uploadFile.getOriginalFilename();
        return storageAdapter.getUrl(bucket, objectName);
    }
}
